package cn.eleven.controller;

import cn.eleven.entity.ForumPost;
import cn.eleven.mapper.ForumPostMapper;
import cn.eleven.common.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin("*")
@RestController
@RequestMapping("/api/forum_post")
public class ForumPostController {

    @Autowired
    private ForumPostMapper forumPostMapper;

    @GetMapping("/user/{userId}")
    public Result getForumPostsByUserId(@PathVariable Integer userId) {
        List<ForumPost> forumPosts = forumPostMapper.getForumPostByUserId(userId);
        return Result.success(forumPosts); // 注意这里返回的是列表
    }
    @DeleteMapping("/{id}")
    public Result deleteForumPost(@PathVariable Integer id) {
        boolean success = forumPostMapper.deleteById(id) > 0;
        return success ? Result.success("Post deleted successfully") : Result.error("Delete failed");
    }
    @PutMapping("/{id}")
    public Result updateForumPost(@PathVariable Integer id, @RequestBody ForumPost forumPost) {
        forumPost.setId(id); // 确保请求体中的 ID 与路径变量中的 ID 匹配
        boolean success = forumPostMapper.updateById(forumPost) > 0;
        return success ? Result.success(forumPost) : Result.error("Update failed");
    }
}